package com.tzy.newbank.controller;

import com.tzy.newbank.Pojo.Bankuser;
import com.tzy.newbank.service.IBankuserService;
import com.tzy.newbank.service.impl.BankuserServiceImpl;
import com.tzy.newbank.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Controller
@RequiredArgsConstructor
@Api(tags = "银行用户管理接口文档")
public class BankuserController {

    private final IBankuserService bankuserService;

    @GetMapping("/index")
    public String index() {
        return "index"; // 返回index.jsp
    }

    @GetMapping("/register")
    public String register() {
        return "register"; // 返回register.jsp
    }

    @GetMapping("/mmain3")
    public String mmain3() {
        return "mmain3"; // 返回mmain3.jsp
    }

    @PostMapping("/hi")
    @ResponseBody
    public String hi() {
        return "hi";
    }

    @ApiOperation("存款")
    @PostMapping("/update/deposit")
    public ModelAndView deposit(@RequestParam(name = "money") Double money,
                                HttpSession session) throws BusinessException {
        Bankuser user = (Bankuser) session.getAttribute("user");
        if (bankuserService.deposit(user, money)) {
            double one = bankuserService.inquiry(user);
            session.setAttribute("money", Double.toString(one));
            session.setAttribute("user", user);
            return new ModelAndView("redirect:/mmain3.jsp");
        }
        return new ModelAndView("redirect:/index.jsp");
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public ModelAndView register2(@RequestParam(name = "username") String username,
                                  @RequestParam(name = "userpass") String userpass,
                                  @RequestParam(name = "money") Double money) throws LoginException {
        Bankuser user = new Bankuser(username, userpass, money);
        if (bankuserService.register(user)) {
            return new ModelAndView("redirect:/index.jsp");
        }
        return null;
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public ModelAndView test(@RequestParam(name = "userid") String userid,
                             @RequestParam(name = "userpass") String userpass,
                             HttpSession session) throws LoginException {
        Bankuser user = new Bankuser(userid, userpass);
        if (bankuserService.login(user)) {
            //登录成功了，使用JwtUtils生成jwt令牌，并传递信息。
            HashMap<String, Object> cliams = new HashMap<>();
            cliams.put("name", user.getName());
            cliams.put("password", user.getPassword());
            cliams.put("balance", user.getBalance());
            String jwt = JwtUtils.createJWT(cliams);
            session.setAttribute("jwt", jwt);
            //传递用于页面显示的信息
            double one = bankuserService.inquiry(user);
            session.setAttribute("money", Double.toString(one));
            session.setAttribute("user", user);
            log.info(user.getName());
            log.info(user.getBalance() + "");
            log.info(user.getPassword());
            return new ModelAndView("redirect:/success.jsp");
        }
        return null;
    }

    @ApiOperation("取款")
    @PostMapping("/update/withdrawals")
    public ModelAndView withdrawals(@RequestParam(name = "money") Double money,
                                    HttpSession session) throws BusinessException {
        Bankuser user = (Bankuser) session.getAttribute("user");
        if (bankuserService.withdrawals(user, money)) {
            double one = bankuserService.inquiry(user);
            session.setAttribute("money", Double.toString(one));
            session.setAttribute("user", user);
            return new ModelAndView("redirect:/mmain3.jsp");
        } else return new ModelAndView("redirect:/index.jsp");
    }

    @ApiOperation("销号")
    @PostMapping("/update/destroy")
    public ModelAndView destroy(HttpSession session) throws BusinessException, LoginException {
        Bankuser user = (Bankuser) session.getAttribute("user");
        bankuserService.destroyAccount(user);
        return new ModelAndView("redirect:/index.jsp");
    }

    @ApiOperation("转账")
    @PostMapping("/update/transfer")
    public ModelAndView transfer(@RequestParam(name = "user2") String user2,
                                 @RequestParam(name = "money") Double money,
                                 HttpSession session) throws BusinessException, TransferErrorException {
        Bankuser user = (Bankuser) session.getAttribute("user");
        Bankuser user3 = new Bankuser(user2);
        double money2 = money;
        if (bankuserService.transfer(user, user3, money2)) {
            double one = bankuserService.inquiry(user);
            session.setAttribute("money", Double.toString(one));
            session.setAttribute("user", user);
            return new ModelAndView("redirect:/mmain3.jsp");
        } else return new ModelAndView("redirect:/index.jsp");
    }

    @ApiOperation("获取信息")
    @PostMapping("/update/getInfo")
    public ModelAndView getinfo(HttpSession session) throws TransferErrorException, NoPowerException, BusinessException {
        Bankuser user = (Bankuser) session.getAttribute("user");
        List<Bankuser> peoples = bankuserService.seleteAll();
        session.setAttribute("user", user);
        session.setAttribute("peoples", peoples);
        return new ModelAndView("redirect:/rootPage.jsp");
    }

    @ApiOperation("冻结")
    @PostMapping("/update/freeze")
    public ModelAndView freeze(@RequestParam(name = "user2") String user2,
                               HttpSession session) throws TransferErrorException, NoPowerException, BusinessException {
        Bankuser user = (Bankuser) session.getAttribute("user");
        Bankuser user3 = new Bankuser(user2);
        if (bankuserService.freeze(user, user3)) {
            double one = bankuserService.inquiry(user);
            List<Bankuser> peoples = bankuserService.seleteAll();
            session.setAttribute("money", Double.toString(one));
            session.setAttribute("user", user);
            session.setAttribute("peoples", peoples);
            return new ModelAndView("redirect:/rootPage.jsp");
        } else return new ModelAndView("redirect:/index.jsp");
    }

    @ApiOperation("解冻")
    @PostMapping("/update/unfreeze")
    public ModelAndView unfreeze(@RequestParam(name = "user2") String user2,
                                 HttpSession session) throws TransferErrorException, NoPowerException, BusinessException {
        Bankuser user = (Bankuser) session.getAttribute("user");
        Bankuser user3 = new Bankuser(user2);
        if (bankuserService.unfreeze(user, user3)) {
            double one = bankuserService.inquiry(user);
            List<Bankuser> peoples = bankuserService.seleteAll();
            session.setAttribute("money", Double.toString(one));
            session.setAttribute("user", user);
            session.setAttribute("peoples", peoples);
            return new ModelAndView("redirect:/rootPage.jsp");
        } else return new ModelAndView("redirect:/index.jsp");
    }
}
